import Vue from 'vue';
import VueRouter from 'vue-router';

import routes from './routes';



Vue.use(VueRouter);
const router = new VueRouter({
    routes,
});

/**
 * 路由拦截
 * 权限验证
 */

router.beforeEach(async (to, from, next) => {
    if (to.path != '/Login' && to.path != '/materilsLogin') {
        if (sessionStorage.getItem('token') == null || sessionStorage.getItem('token') == undefined || sessionStorage.getItem('token') == '') {
            sessionStorage.clear();
            // console.log(to.path == '/materilsLogin');
            //   to.path!='/materilsLogin'
            next('/Login')
        } else {
            next();
        }

    } else {
        next();
    }



});

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err)
}

router.afterEach(to => {

    window.scrollTo(0, 0);
});

export default router;
